<?php 
namespace App\Services;

use App\Facades\Infra\AdminManager;
use Auth;
use App\Facades\Utils\ValidateParameter;
use App\Facades\Utils\LogUtil;

class LevelService{
	public function getAll(){
		return AdminManager::getLevels();
	}

	public function create($request){
		$rules = [
			'level_name' => 'required|unique:levels,level_name'
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		AdminManager::createLevel($data);

		LogUtil::createLog(Auth::user()->name,'创建了一个新肥胖等级：'.$data['level_name']);
	}

	public function update($request){
		$rules = [
			'id' => 'required|exists:levels,id',
			'level_name' => 'required|unique:levels,level_name,'.$request->id
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		$id = $data['id'];

		$level = AdminManager::updateLevel($id,$data);

		LogUtil::updateLog(Auth::user()->name,'修改了一个肥胖等级：'.$level->level_name.'->'.$data['level_name']);
	}

	public function delete($request){
		$rules = [
			'id' => 'required|exists:levels,id'
		];

		ValidateParameter::validate($request,$rules);

		$data = $request->all();

		$id = $data['id'];

		$level = AdminManager::deletelevel($id);

		LogUtil::deleteLog(Auth::user()->name,'删除了一个肥胖等级：'.$level->level_name);
	}
}